package com.fivefu.core.skyeye.event.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.core.skyeye.event.entity.DbAiSceneCameraImage;
import com.fivefu.core.skyeye.event.mapper.DbAiSceneCameraImageMapper;
import com.fivefu.core.skyeye.event.services.DbAiSceneCameraImageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 场景探头缩略图 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-09-07 15:03:33
 */
@Service
public class DbAiSceneCameraImageServiceImpl extends ServiceImpl<DbAiSceneCameraImageMapper, DbAiSceneCameraImage> implements DbAiSceneCameraImageService {

    /**
     * 编辑场景探头缩略图
     * @param userid 用户编号
     * @param sceneCameraId 场景探头关联编号
     * @param scope 是否开启区域（0：否 1 ：是）
     * @param scopeArea 区域范围
     * @param image 缩略图
     */
    @Override
    public void addSceneCameraImage(String userid, String sceneCameraId, Integer scope, String scopeArea, String image) {
        QueryWrapper<DbAiSceneCameraImage> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("scene_camera_id",sceneCameraId);
        wrapper.eq("image",image);
        DbAiSceneCameraImage dbAiSceneCameraImage = baseMapper.selectOne(wrapper);
        DbAiSceneCameraImage sceneCameraImage = new DbAiSceneCameraImage();
        sceneCameraImage.setUpdatedBy(userid);
        sceneCameraImage.setUpdatedTime(LocalDateTime.now());
        sceneCameraImage.setIsDelete(0);
        sceneCameraImage.setSceneCameraId(sceneCameraId);//场景探头关联编号
        sceneCameraImage.setScope(scope);//是否限定区域;0:否,1:是
        sceneCameraImage.setScopeArea(scopeArea);//区域
        sceneCameraImage.setImage(image);//缩略图
        if (dbAiSceneCameraImage == null){
            sceneCameraImage.setCreatedBy(userid);
            sceneCameraImage.setCreatedTime(LocalDateTime.now());
            baseMapper.insert(sceneCameraImage);
        }else {
            sceneCameraImage.setId(dbAiSceneCameraImage.getId());
            baseMapper.updateById(sceneCameraImage);
        }

    }

    /**
     * 查找场景探头历史识别区域图
     * @param sceneCameraId
     * @return
     */
    @Override
    public String getSceneCameraImageById(String sceneCameraId){
        String imgStr = "";
        QueryWrapper<DbAiSceneCameraImage> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("scene_camera_id",sceneCameraId);
        wrapper.orderByDesc("created_time");
        List<DbAiSceneCameraImage> dbAiSceneCameraImages = baseMapper.selectList(wrapper);
        for (DbAiSceneCameraImage dbAiSceneCameraImage : dbAiSceneCameraImages) {
            imgStr += dbAiSceneCameraImage.getImage()+",";
        }
        if (StrUtils.isNotNull(imgStr)){
            imgStr= imgStr.substring(0, imgStr.length() - 1);
        }
        return imgStr;
    }

    /**
     * 根据场景探头关联表id查找相关历史识别区域图
     * @param sceneCameraId  场景探头关联表id
     * @return
     */
    @Override
    public List<DbAiSceneCameraImage> getSceneCameraImageList(String sceneCameraId) {
        QueryWrapper<DbAiSceneCameraImage> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("scene_camera_id",sceneCameraId);
        wrapper.orderByDesc("created_time");
        return baseMapper.selectList(wrapper);
    }
}
